package cc.mrbird.febs.common.formatter;

import cc.mrbird.febs.common.enums.SensitiveType;
import cc.mrbird.febs.common.utils.DesensitizedUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.util.Locale;


public class DesensitizedFormatterOfString implements Formatter<String> {
    private SensitiveType type;

    public SensitiveType getTypeEnum() {
        return type;
    }

    public void setTypeEnum(SensitiveType type) {
        this.type = type;
    }

    @Override
    public String parse(String valueStr, Locale locale) throws ParseException {
        if (StringUtils.isNotBlank(valueStr)) {
            switch (type) {
                case ID_CARD:
                    valueStr = DesensitizedUtils.idCardNum(valueStr);
                    break;
                case MOBILE:
                    valueStr = DesensitizedUtils.mobilePhone(valueStr);
                    break;
                case POLITICS:
                    valueStr = DesensitizedUtils.politics(valueStr);
                    break;
                case ADDRESS:
                    valueStr = DesensitizedUtils.address(valueStr, valueStr.length());
                    break;
                case CULTURE:
                    valueStr = DesensitizedUtils.culture(valueStr);
                    break;
                default:
            }
        }
        return valueStr;
    }

    @Override
    public String print(String s, Locale locale) {
        return s;
    }
}